package com.example.algorithm.utils;

import com.example.algorithm.pojo.TimeInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class GetDateUtils {
    public static String getIncrementDate(String startDate) {
        //根据传入的日期字符串，操作参数("+","-")，加数(要操作的天数)，对日期进行加减操作
        // 转化原时间的的格式(发现用带'-'的字符串进行Date.parse() 时会报错，经过这样替换可以实现)
        // String str = startDate.replace("-", "/");

        // 加减时间类型
        String operator = "+";

        // 加减的天数
        Integer i = -30;

        // 定义变量接收转化的时间值
        long j = 0;
        Date date = null;

        try {
            j = Date.parse(startDate);
            date = new Date(j);
        } catch (Exception e) {
            e.printStackTrace();
        }
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);

        if ("+".equals(operator)) {
            gc.add(GregorianCalendar.DATE, i);

        } else if ("-".equals(operator)) {
            gc.add(GregorianCalendar.DATE, (-i));
        }

        Date a = gc.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd ");

        return sdf.format(a);
    }

    public static String getStockDate(String startDate) {
        // 加减时间类型
        String operator = "+";

        // 加减的天数
        Integer i = -365;

        // 定义变量接收转化的时间值
        long j = 0;
        Date date = null;

        try {
            j = Date.parse(startDate);
            date = new Date(j);
        } catch (Exception e) {
            e.printStackTrace();
        }
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);

        if ("+".equals(operator)) {
            gc.add(GregorianCalendar.DATE, i);

        } else if ("-".equals(operator)) {
            gc.add(GregorianCalendar.DATE, (-i));
        }

        Date a = gc.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd ");

        return sdf.format(a);
    }

    public static String manyDate(String startDate, int i) {
        //根据传入的日期字符串，操作参数("+","-")，加数(要操作的天数)，对日期进行加减操作
        // 转化原时间的的格式(发现用带'-'的字符串进行Date.parse() 时会报错，经过这样替换可以实现)
        // String str = startDate.replace("-", "/");

        // 加减时间类型
        String operator = "+";


        // 定义变量接收转化的时间值
        long j = 0;
        Date date = null;

        try {
            j = Date.parse(startDate);
            date = new Date(j);
        } catch (Exception e) {
            e.printStackTrace();
        }
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);

        if ("+".equals(operator)) {
            gc.add(GregorianCalendar.DATE, i);

        } else if ("-".equals(operator)) {
            gc.add(GregorianCalendar.DATE, (-i));
        }

        Date a = gc.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd ");

        return sdf.format(a);
    }

    public static int differentDay(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)) + 1;
        return days;
    }

    //得到年月
    public static TimeInfo getTime(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String dateStr = sdf.format(date);
        Integer year = Integer.parseInt(dateStr.substring(0, 4));
        Integer month = Integer.parseInt(dateStr.substring(5, 7));
        TimeInfo timeInfo = new TimeInfo();
        timeInfo.setYear(year);
        timeInfo.setMonth(month);
        return timeInfo;
    }

    /**
     * 获取某年第一天日期
     *
     * @param year
     *            年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }
}
